﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="Class1.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：Class1.cs
// 创建时间：2014-05-13 14:20
// 修改时间：2014-05-13 14:20
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System.Web.UI.WebControls;

namespace WebCommon
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Newtonsoft.Json;

    /// <summary>
    /// 
    /// </summary>
    public static class PageJs
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static PageListJs2<T> ToPageList<T>(this IEnumerable<T> list, int rows, int page) where T : class
        {
            var skip = rows * (page - 1);
            var enumerable = list as T[] ?? list.ToArray();
            var dlist = enumerable.Skip(skip).Take(rows).ToList();
            var pl2 = new PageListJs2<T>(dlist, rows, page);
            pl2.Count = enumerable.Count();
            return pl2;
        }
    }
    [JsonObject(MemberSerialization.OptIn)]
    public class PageListJs2<T>
        where T : class
    {

        /// <summary>
        /// js分页
        /// </summary>
        /// <param name="list"></param>
        /// <param name="rows">每行多少个</param>
        /// <param name="page">第几页</param>
        public PageListJs2(List<T> list, int rows, int page)
        {

            this.Page = page;
            this.Rows = rows;


            this.DataList = list.ToList(); 
        }

        /// <summary>
        /// 每行多少
        /// </summary>
        [JsonProperty("rows")]
        public int Rows { get; set; }
        /// <summary>
        /// 第几页
        /// </summary>
        [JsonProperty("page")]
        public int Page { get; set; }



        /// <summary>
        /// 转化类型
        /// </summary>
        /// <typeparam name="TU"></typeparam>
        /// <param name="func"></param>
        /// <returns></returns>
        public PageListJs2<TU> OpData<TU>(Func<T, TU> func) where TU : class
        {
            var pl = new PageListJs2<TU>(this.DataList.Select(func).ToList(), Rows, Page);
            pl.Count = this.Count;
         
            return pl;
        }
        /// <summary>
        /// 总数量
        /// </summary>
        [JsonProperty("count")]
        public int Count { get; set; }

        /// <summary>
        /// 分页数据
        /// </summary>
        [JsonProperty("datalist")]
        public List<T> DataList { get; set; }
    }

    /// <summary>
    /// s
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [JsonObject(MemberSerialization.OptIn)]
    public class PageListJs<T>
        where T : class
    {
        /// <summary>
        /// 分页
        /// </summary>
        public PageListJs()
        {

        }

        /// <summary>
        /// js分页
        /// </summary>
        /// <param name="list"></param>
        /// <param name="rows">每行多少个</param>
        /// <param name="page">第几页</param>
        public PageListJs(IEnumerable<T> list, int rows, int page)
        {

            this.StartPage(list, rows, page);
        }

        /// <summary>
        /// 每行多少
        /// </summary>
        [JsonProperty("rows")]
        public int Rows { get; set; }
        /// <summary>
        /// 第几页
        /// </summary>
        [JsonProperty("page")]
        public int Page { get; set; }

        /// <summary>
        /// 开始分页
        /// </summary>
        /// <param name="list"></param>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        private void StartPage(IEnumerable<T> list, int rows, int page)
        {
            this.Page = page;
            this.Rows = rows;

            // 跳过多少行
            var skip = rows * (page - 1);
            var enumerable = list as T[] ?? list.ToArray();
            this.DataList = enumerable.Skip(skip).Take(rows).ToList();
            this.Count = enumerable.Count();
        }

        /// <summary>
        /// 转化类型
        /// </summary>
        /// <typeparam name="TU"></typeparam>
        /// <param name="func"></param>
        /// <returns></returns>
        public PageListJs<TU> OpData<TU>(Func<T, TU> func) where TU : class
        {
            var pl = new PageListJs<TU>();
            pl.Count = this.Count;
            pl.DataList = this.DataList.Select(func).ToList();
            pl.Rows = this.Rows;
            pl.Page = this.Page;

            return pl;
        }
        /// <summary>
        /// 总数量
        /// </summary>
        [JsonProperty("count")]
        public int Count { get; set; }

        /// <summary>
        /// 分页数据
        /// </summary>
        [JsonProperty("datalist")]
        public List<T> DataList { get; set; }
    }

    /// <summary>
    /// 分页类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [JsonObject(MemberSerialization.OptIn)]
    public class PageList<T>
        where T : class
    {
        #region Fields

        /// <summary>
        ///     The list.
        /// </summary> 
        public IEnumerable<T> SetList { get; set; }

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PageList{T}"/> class.
        /// </summary>
        /// <param name="list">
        /// 需要分页的list
        /// </param>
        public PageList(IEnumerable<T> list)
        {
            this.ViewPageNum = 2;
            this.SetList = list;
        }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="list">数据list</param>
        /// <param name="viewPageNum">View显示的页数（半数）</param>
        public PageList(IEnumerable<T> list, int viewPageNum)
            : this(list)
        {
            this.ViewPageNum = viewPageNum;

        }


        #endregion

        #region Public Properties
        /// <summary>
        /// 每页大小
        /// </summary>
        [JsonProperty("rows")]
        public int Rows { get; private set; }

        /// <summary>
        /// Gets a value indicating whether is equal max.
        /// </summary>
        [JsonProperty("isequalmax")]
        public bool IsEqualMax { get; private set; }

        /// <summary>
        /// Gets a value indicating whether is equal min.
        /// </summary>
        [JsonProperty("isequalmin")]
        public bool IsEqualMin { get; private set; }

        /// <summary>
        /// 是否等于当前页码
        /// </summary>
        [JsonProperty("isequalpage")]
        public bool IsEqualPage { get; private set; }

        /// <summary>
        ///     最大页码
        /// </summary>
        [JsonProperty("maxpages")]
        public int MaxPages { get; private set; }

        /// <summary>
        ///     最小页码
        /// </summary>
        [JsonProperty("minpages")]
        public int MinPages { get; private set; }

        /// <summary>
        ///     当前页码
        /// </summary>
        [JsonProperty("page")]
        public int Page { get; private set; }

        /// <summary>
        /// 分页好的数据列表
        /// </summary>
        [JsonProperty("datalist")]
        public IEnumerable<object> DataList { get; private set; }

        /// <summary>
        ///     总行数
        /// </summary>
        [JsonProperty("records")]
        public int Records { get; private set; }

        /// <summary>
        ///     总页数
        /// </summary>
        [JsonProperty("total")]
        public int Total { get; private set; }

        /// <summary>
        /// 上一页页码
        /// </summary>
        [JsonProperty("up")]
        public int Up { get; private set; }

        /// <summary>
        /// 下一页页码
        /// </summary>
        [JsonProperty("next")]
        public int Next { get; private set; }

        /// <summary>
        /// 显示页码集合
        /// </summary>
        [JsonProperty("viewpages")]
        public int[] Viewpages { get; private set; }

        /// <summary>
        /// 分页数量（取半数）
        /// </summary>
        public int ViewPageNum { get; set; }

        #endregion


        #region Public Methods and Operators


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="rows">
        /// 每页多少行
        /// </param>
        /// <param name="page">
        /// 第几页
        /// </param>
        protected virtual void StartPage(int rows, int page)
        {
            this.Page = page;
            this.Rows = rows;

            // 跳过多少行
            var skip = rows * (page - 1);
            this.Records = this.SetList.Count();

            this.DataList = this.SetList.Skip(skip).Take(rows).ToList();

            // 总页数
            this.Total = (this.Records + rows - 1) / rows;
        }

        /// <summary>
        /// 对dataList进行操作
        /// </summary>
        /// <param name="dataList"></param>
        public void OpData(Func<T, object> dataList)
        {
            var objects = new List<object>();
            foreach (var o in this.DataList)
            {
                var obj = dataList(o as T);
                objects.Add(obj);
            }

            this.DataList = objects;
        }

        /// <summary>
        /// 开始分页
        /// </summary>
        /// <param name="rows">
        /// 每页多少行
        /// </param>
        /// <param name="page">
        /// 第几页
        /// </param>

        public void StartPages(int rows, int page)
        {
            this.StartPage(rows, page);
            this.ViewPage();
        }

        /// <summary>
        /// 生成View数据
        /// </summary> 
        protected virtual void ViewPage()
        {
            this.MinPages = this.Page - this.ViewPageNum;

            this.MinPages = this.MinPages < 1 ? 1 : this.MinPages;

            this.MaxPages = this.Page + this.ViewPageNum;

            this.MaxPages = this.MaxPages > this.Total ? this.Total : this.MaxPages;
            this.IsEq();
            this.SetUpNext();

            this.Viewpages = new int[this.MaxPages - this.MinPages + 1];
            for (var i = 0; i < this.Viewpages.Length; i++)
            {
                this.Viewpages[i] = i + this.MinPages;
            }
        }

        /// <summary>
        /// 更新是否最大页
        /// </summary>
        private void IsEq()
        {
            this.IsEqualMax = this.Page == this.Total;

            this.IsEqualMin = this.Page == 1;
        }

        /// <summary>
        /// 更新上一下，下一页
        /// </summary>
        private void SetUpNext()
        {
            this.Up = this.Page - 1 <= 1 ? 1 : this.Page - 1;
            this.Next = this.Page + 1 >= this.Total ? this.Total : this.Page + 1;
        }

        #endregion

    }
}